﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using MODEL;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 新闻类别
    /// </summary>
    public class NewsColumn
    {
        public NewsColumn()
        { }
        /// <summary>
        /// 新闻类别 添加
        /// </summary>
        /// <param name="model">新闻类别实体</param>
        /// <returns>是否执行成功</returns>
        public bool NewsColumnAdd(MODEL.NewsColumn model)
        {
            SqlParameter[] parameter ={new SqlParameter("@name",SqlDbType.VarChar,60),
                                       new SqlParameter("@readme",SqlDbType.VarChar,200),
                                       new SqlParameter("@parentid",SqlDbType.Int),
                                       new SqlParameter("@depth",SqlDbType.SmallInt),
                                       new SqlParameter("@rootid",SqlDbType.Int),
                                       new SqlParameter("@childnum",SqlDbType.SmallInt),
                                       new SqlParameter("@previd",SqlDbType.Int),
                                       new SqlParameter("@nextid",SqlDbType.Int),
                                       new SqlParameter("@orderid",SqlDbType.SmallInt),
                                       new SqlParameter("@parentpath",SqlDbType.VarChar,40),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.NclassName;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Nreadme;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.NparentID;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.Ndepth;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.NrootID;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.NchildNum;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.NprevID;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Value = model.NnextID;
            parameter[7].Direction = ParameterDirection.Input;
            parameter[8].Value = model.NorderID;
            parameter[8].Direction = ParameterDirection.Input;
            parameter[9].Value = model.NparentPath;
            parameter[9].Direction = ParameterDirection.Input;
            parameter[10].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewscolumnAdd", parameter);
        }
        /// <summary>
        /// 新闻类别 删除
        /// </summary>
        /// <param name="columnId">类别编号</param>
        /// <returns>是否执行成功</returns>
        public bool NewsColumnDel(int columnId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.Int),
                                      new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = columnId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewscolumnDelete", parameter);
        }
        /// <summary>
        /// 新闻类别 修改
        /// </summary>
        /// <param name="model">新闻类别实体</param>
        /// <returns>是否执行成功</returns>
        public bool NewsColumnUpdate(MODEL.NewsColumn model)
        {
            SqlParameter[] parameter ={new SqlParameter("@name",SqlDbType.VarChar,60),
                                       new SqlParameter("@readme",SqlDbType.VarChar,200),
                                       new SqlParameter("@parentid",SqlDbType.Int),
                                       new SqlParameter("@depth",SqlDbType.SmallInt),
                                       new SqlParameter("@rootid",SqlDbType.Int),
                                       new SqlParameter("@childnum",SqlDbType.SmallInt),
                                       new SqlParameter("@previd",SqlDbType.Int),
                                       new SqlParameter("@nextid",SqlDbType.Int),
                                       new SqlParameter("@orderid",SqlDbType.SmallInt),
                                       new SqlParameter("@parentpath",SqlDbType.VarChar,40),
                                       new SqlParameter("@Return",SqlDbType.Int),
                                       new SqlParameter("@id",SqlDbType.Int)};
            parameter[0].Value = model.NclassName;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.Nreadme;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.NparentID;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.Ndepth;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.NrootID;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.NchildNum;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.NprevID;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Value = model.NnextID;
            parameter[7].Direction = ParameterDirection.Input;
            parameter[8].Value = model.NorderID;
            parameter[8].Direction = ParameterDirection.Input;
            parameter[9].Value = model.NparentPath;
            parameter[9].Direction = ParameterDirection.Input;
            parameter[10].Direction = ParameterDirection.ReturnValue;
            parameter[11].Value = model.NclassID;
            parameter[11].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedure("ProNewscolumnUpdate", parameter);
        }
        /// <summary>
        /// 新闻类别 列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet GetNewsColumnList()
        {
            return DbHelper.RunProcedure("ProNewscolumnSelectAll");
        }
        /// <summary>
        /// 新闻类别 实体
        /// </summary>
        /// <param name="columnId">类别编号</param>
        /// <returns>实体</returns>
        public MODEL.NewsColumn GetNewsColumnModel(int columnId)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.Int) };
            parameter[0].Value = columnId;
            parameter[0].Direction = ParameterDirection.Input;
            DataSet ds = DbHelper.RunProcedureS("ProNewscolumnSelectByID", parameter);
            MODEL.NewsColumn model = new MODEL.NewsColumn();
            model.NclassID = columnId;
            model.NclassName = ds.Tables[0].Rows[0]["nclassname"].ToString();
            model.Nreadme = ds.Tables[0].Rows[0]["nreadme"].ToString();
            model.NparentID = Convert.ToInt32(ds.Tables[0].Rows[0]["nparentid"]);
            model.Ndepth = Convert.ToInt32(ds.Tables[0].Rows[0]["ndepth"]);
            model.NrootID = Convert.ToInt32(ds.Tables[0].Rows[0]["nrootid"]);
            model.NchildNum = Convert.ToInt32(ds.Tables[0].Rows[0]["nchildnum"]);
            model.NprevID = Convert.ToInt32(ds.Tables[0].Rows[0]["nprevid"]);
            model.NnextID = Convert.ToInt32(ds.Tables[0].Rows[0]["nnextid"]);
            model.NorderID = Convert.ToInt32(ds.Tables[0].Rows[0]["norderid"]);
            model.NparentPath = ds.Tables[0].Rows[0]["nparentpath"].ToString();
            return model;
        }
    }
    /// <summary>
    /// 新闻信息
    /// </summary>
    public class NewsInformation
    {
        public NewsInformation()
        { }
        /// <summary>
        /// 新闻信息 添加
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationAdd(MODEL.NewsInformation model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@classid",SqlDbType.Int),
                                       new SqlParameter("@title",SqlDbType.VarChar,50),
                                       new SqlParameter("@key",SqlDbType.VarChar,50),
                                       new SqlParameter("@subtitle",SqlDbType.VarChar,50),
                                       new SqlParameter("@abstract",SqlDbType.VarChar,200),
                                       new SqlParameter("@author",SqlDbType.VarChar,20),
                                       new SqlParameter("@content",SqlDbType.Text),
                                       new SqlParameter("@isfrom",SqlDbType.VarChar,200),
                                       new SqlParameter("@isimg",SqlDbType.Bit),
                                       new SqlParameter("@ishot",SqlDbType.Bit),
                                       new SqlParameter("@url",SqlDbType.VarChar,200),
                                       new SqlParameter("@operator",SqlDbType.Char,5),
                                       new SqlParameter("@clicknum",SqlDbType.Int),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.NID;
            parameter[1].Value = model.NclassID;
            parameter[2].Value = model.Ntitle;
            parameter[3].Value = model.Nkey;
            parameter[4].Value = model.Nsubtitle;
            parameter[5].Value = model.Nabstract;
            parameter[6].Value = model.Nauthor;
            parameter[7].Value = model.Ncontent;
            parameter[8].Value = model.Nisfrom;
            parameter[9].Value = model.Nisimg;
            parameter[10].Value = model.Nishot;
            parameter[11].Value = model.Nurl;
            parameter[12].Value = model.Noperator;
            parameter[13].Value = model.NclickNum;
            for (int i = 0; i < 14; i++)
            {
                parameter[i].Direction = ParameterDirection.Input;
            }
            parameter[14].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationAdd", parameter);
        }
        /// <summary>
        /// 新闻信息 删除
        /// </summary>
        /// <param name="newsId">新闻编号</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationDel(string newsId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = newsId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationDelete", parameter);
        }
        /// <summary>
        /// 新闻信息 修改
        /// </summary>
        /// <param name="model">信息实体</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationUpdate(MODEL.NewsInformation model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@classid",SqlDbType.Int),
                                       new SqlParameter("@title",SqlDbType.VarChar,50),
                                       new SqlParameter("@key",SqlDbType.VarChar,50),
                                       new SqlParameter("@subtitle",SqlDbType.VarChar,50),
                                       new SqlParameter("@abstract",SqlDbType.VarChar,200),
                                       new SqlParameter("@author",SqlDbType.VarChar,20),
                                       new SqlParameter("@content",SqlDbType.Text),
                                       new SqlParameter("@isfrom",SqlDbType.VarChar,200),
                                       new SqlParameter("@ishot",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.NID;
            parameter[1].Value = model.NclassID;
            parameter[2].Value = model.Ntitle;
            parameter[3].Value = model.Nkey;
            parameter[4].Value = model.Nsubtitle;
            parameter[5].Value = model.Nabstract;
            parameter[6].Value = model.Nauthor;
            parameter[7].Value = model.Ncontent;
            parameter[8].Value = model.Nisfrom;
            parameter[9].Value = model.Nishot;
            for (int i = 0; i < 9; i++)
            {
                parameter[i].Direction = ParameterDirection.Input;
            }
            parameter[10].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationUpdate", parameter);
        }
        /// <summary>
        /// 新闻信息 修改是否热点
        /// </summary>
        /// <param name="newId">新闻编号</param>
        /// <param name="isHot">是否热点</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationUpdateIshot(string newId, bool isHot)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@ishot",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = newId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = isHot;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationUpdateIshot", parameter);
        }
        /// <summary>
        /// 新闻信息 修改是否置顶
        /// </summary>
        /// <param name="newId">新闻编号</param>
        /// <param name="isTop">是否置顶</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationUpdateIsimg(string newId, bool isTop)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@istop",SqlDbType.Bit),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = newId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = isTop;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationUpdateIsimg", parameter);
        }
        /// <summary>
        /// 新闻信息 修改新闻类别
        /// </summary>
        /// <param name="newId">新闻编号</param>
        /// <param name="classId">类别编号</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationUpdateClassid(string newId, int classId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@classid",SqlDbType.Int),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = newId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = classId;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationUpdateClassid", parameter);
        }
        /// <summary>
        /// 新闻信息 修改新闻标题
        /// </summary>
        /// <param name="newId">新闻编号</param>
        /// <param name="newTitle">新闻标题</param>
        /// <returns>是否执行成功</returns>
        public bool NewsInformationUpdateTitle(string newId, string newTitle)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,12),
                                       new SqlParameter("@title",SqlDbType.VarChar,50),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = newId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = newTitle;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProNewsinformationUpdateTitle", parameter);
        }
        /// <summary>
        /// 新闻信息 点击量+1
        /// </summary>
        /// <param name="newsId">新闻编号</param>
        public void NewsClickNumAdd(string newsId)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 12) };
            parameter[0].Value = newsId;
            parameter[0].Direction = ParameterDirection.Input;
            DbHelper.RunProcedure("ProNewsinformationUpdateClicknumByID", parameter);
        }
        /// <summary>
        /// 新闻信息 列表
        /// </summary>
        /// <returns>列表</returns>
        public DataSet GetNewsInformationList()
        {
            return DbHelper.RunProcedure("ProNewsinformationSelectAll");
        }
        /// <summary>
        /// 新闻信息 列表
        /// </summary>
        /// <param name="columnId">新闻编号</param>
        /// <returns>列表</returns>
        public DataSet GetNewsInformationList(int columnId)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.Int) };
            parameter[0].Value = columnId;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProNewsinformationSelectByClassid", parameter);
        }
        /// <summary>
        /// 新闻信息 列表
        /// </summary>
        /// <param name="timeYear">年</param>
        /// <returns>列表</returns>
        public DataSet GetNewsInformationList(string timeYear)
        {
            SqlParameter[] parameter = { new SqlParameter("@year", SqlDbType.Char, 4) };
            parameter[0].Value = timeYear;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProNewsinformationSelectByYear", parameter);
        }
        /// <summary>
        /// 新闻信息 列表
        /// </summary>
        /// <param name="timeYear">年</param>
        /// <param name="timeMonth">月</param>
        /// <returns>列表</returns>
        public DataSet GetNewsInformationList(string timeYear, string timeMonth)
        {
            SqlParameter[] parameter ={new SqlParameter("@year",SqlDbType.Char,4),
                                       new SqlParameter("@month",SqlDbType.Char,2)};
            parameter[0].Value = timeYear;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = timeMonth;
            parameter[1].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProNewsinformationSelectByMonth", parameter);
        }
        /// <summary>
        /// 新闻信息 列表
        /// </summary>
        /// <param name="timeYear">年</param>
        /// <param name="timeMonth">月</param>
        /// <param name="timeDay">日</param>
        /// <returns>列表</returns>
        public DataSet GetNewsInformationList(string timeYear, string timeMonth, string timeDay)
        {
            SqlParameter[] parameter ={new SqlParameter("@year",SqlDbType.Char,4),
                                      new SqlParameter("@month",SqlDbType.Char,2),
                                      new SqlParameter("@day",SqlDbType.Char,2)};
            parameter[0].Value = timeYear;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = timeMonth;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = timeDay;
            parameter[2].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProNewsinformationSelectByTime", parameter);
        }
        /// <summary>
        /// 新闻信息 实体
        /// </summary>
        /// <param name="newsId">新闻编号</param>
        /// <returns>实体</returns>
        public MODEL.NewsInformation GetNewsInformationModel(string newsId)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 12) };
            parameter[0].Value = newsId;
            parameter[0].Direction = ParameterDirection.Input;
            DataSet ds = DbHelper.RunProcedureS("ProNewsinformationSelectByID", parameter);
            MODEL.NewsInformation model = new MODEL.NewsInformation();
            model.NID = ds.Tables[0].Rows[0]["nid"].ToString();
            model.NclassID = Convert.ToInt32(ds.Tables[0].Rows[0]["nclassid"]);
            model.Ntitle = ds.Tables[0].Rows[0]["ntitle"].ToString();
            model.Nkey = ds.Tables[0].Rows[0]["nkey"].ToString();
            model.Nsubtitle = ds.Tables[0].Rows[0]["nsubtitle"].ToString();
            model.Nabstract = ds.Tables[0].Rows[0]["nabstract"].ToString();
            model.Nauthor = ds.Tables[0].Rows[0]["nauthor"].ToString();
            model.Ncontent = ds.Tables[0].Rows[0]["ncontent"].ToString();
            model.Nyear = ds.Tables[0].Rows[0]["nyear"].ToString();
            model.Nmonth = ds.Tables[0].Rows[0]["nmonth"].ToString();
            model.Nday = ds.Tables[0].Rows[0]["nday"].ToString();
            model.Ntimestamp = ds.Tables[0].Rows[0]["ntimestamp"].ToString();
            model.Nisfrom = ds.Tables[0].Rows[0]["nisfrom"].ToString();
            model.Nisimg = Convert.ToBoolean(ds.Tables[0].Rows[0]["nisimg"]);
            model.Nishot = Convert.ToBoolean(ds.Tables[0].Rows[0]["nishot"]);
            model.Nurl = ds.Tables[0].Rows[0]["nurl"].ToString();
            model.Noperator = ds.Tables[0].Rows[0]["noperator"].ToString();
            model.NclickNum = Convert.ToInt32(ds.Tables[0].Rows[0]["nclicknum"]);
            model.Ntime = Convert.ToDateTime(ds.Tables[0].Rows[0]["Ntime"]);
            return model;
        }
        /// <summary>
        /// 新闻信息 编号
        /// </summary>
        /// <returns>最大编号</returns>
        public string GetMaxId()
        {
            DateTime newTime = DateTime.Now;
            string yearTime = newTime.Year.ToString();
            string monthTime = newTime.Month.ToString();
            string dayTime = newTime.Day.ToString();
            string sqlStr = "select count(*) from g_newsinformation where nyear='" + yearTime + "' and nmonth='" + monthTime + "' and nday='" + dayTime + "'";
            DataSet ds = DbHelper.Query(sqlStr);
            return ds.Tables[0].Rows[0][0].ToString();
        }
    }
}
